const orderDetailController = require('./orderDetail.controller')
orderCtroller.$inject = [
    '$rootScope',
    '$scope',
    '$location',
    //'$routeParams',
    'orderService',
    '$uibModal',
    '$log'
]

function orderCtroller ($rootScope,$scope, $location, OrderSvc,$modal,$log){
    console.log("foodCtroller starting .......rewqqqqwerqwr");
    var oc = this;
    oc.orders = [];
    oc.totalPrice = 0;
    oc.noNumber = 0;
    // oc.rejects = [];
    // oc.receive = [];

    OrderSvc.getMerchantId(function(err,data){
        if(!err){
            console.log($rootScope.merchant._id)
            OrderSvc.getOrderlistByMerchantId($rootScope.merchant._id,function(err,data){
                oc.orders = data;
                oc.orders.reverse();
                angular.forEach(oc.orders, function(item){
                    if(item.status=="等待商家接单"){
                        oc.noNumber += 1;
                    }
                })
            });
        }
    })
    
    oc.refresh = function(){
        console.log($rootScope.merchant._id)
        OrderSvc.getOrderlistByMerchantId($rootScope.merchant._id,function(err,data){
            oc.orders = data;
            oc.orders.reverse();
        });
    }

    oc.detail = function(item){
        console.log(item._id);
        // let modalInstance = $modal.open({
        //     templateUrl : require('../html/updateFood.html')
        // })
        var modalInstance = $modal.open({  
            template : require('../html/orderDetail.html'),  
            controller : orderDetailController,  
            resolve : {  
                orderDetail : function() {
                    return item;  
                },
                refresh : function(){
                    return oc.refresh;
                }  
            }  
        });  
        
        modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
            console.log('modal is opened');
        });  
        modalInstance.result.then(function(result) {  
            console.log(result);  
        }, function(reason) {  
            console.log(reason);// 点击空白区域，总会输出backdrop  
            // click，点击取消，则会暑促cancel  
            $log.info('Modal dismissed at: ' + new Date());  
        }); 
    }
}

module.exports = orderCtroller;